﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SPPivotViewer.HttpHandler.Internal
{
    public class SPAllListCollectionFactory
    {
        public Collection MakeCollection(CollectionRequestContext context, string collectionFileName)
        {
            Collection ColReturn = new Collection();
            SPWeb curWeb = SPContext.Current.Web;
            SPSite curSite = SPContext.Current.Site;
            foreach (SPList list in curWeb.Lists)
            {
                if (list.Hidden)
                    continue;
                if (string.IsNullOrEmpty(list.ImageUrl))
                    continue;
                List<Facet> lstFacet=new List<Facet>();
                string strPictuLib = "";
                string strType = Enum.GetName(typeof(SPListTemplateType), list.BaseTemplate);
                if(string.IsNullOrEmpty(strType))
                    strType=Enum.GetName(typeof(SPBaseType), list.BaseType);
                if (!string.IsNullOrEmpty(strType))
                {
                    if (list is SPDocumentLibrary)
                    {
                        SPDocumentLibrary docLib = list as SPDocumentLibrary;
                        lstFacet.Add(new Facet("Type", strType));
                        lstFacet.Add(new Facet("SharePoint Object Type", "Document Library"));
                        lstFacet.Add(new Facet("nb items", list.ItemCount));
                    }
                    else
                    {
                        lstFacet.Add(new Facet("Type", strType));
                        lstFacet.Add(new Facet("nb items", list.ItemCount));
                        lstFacet.Add(new Facet("SharePoint Object Type", "List"));

                    }
                    if (list is SPPictureLibrary)
                        strPictuLib = "&ImageField=Item%20Url";
                    List<string> strContentTypes=new List<string>();
                    foreach (SPContentType ct in list.ContentTypes)
                        strContentTypes.Add(ct.Name);
                    lstFacet.Add(new Facet("Content Type", strContentTypes.ToArray()));

                }
                string listColUrl=null;
                if(list.ItemCount>0)
                    listColUrl=curWeb.Url + "/" + list.Title + ".cxml?Auto=true" + strPictuLib;
                ColReturn.AddItem(list.Title, listColUrl, list.Description, new ItemImage(new Uri(curWeb.Url + list.ImageUrl)), lstFacet.ToArray());
            
            }
            SPWebTemplateCollection templateCol= curSite.GetWebTemplates((uint)curWeb.Locale.LCID);

            foreach (SPWeb web in curWeb.Webs)
            {
                List<Facet> lstFacet = new List<Facet>();
                SPWebTemplate curTemplate = (from SPWebTemplate t in templateCol where t.ID == web.WebTemplateId select t).FirstOrDefault();
               
                if (curTemplate.IsHidden)
                    continue;
                lstFacet.Add(new Facet("SharePoint Object Type", "Site"));
                lstFacet.Add(new Facet("Type", curTemplate.Title));
                lstFacet.Add(new Facet("nb items", web.Lists.Count));
                ColReturn.AddItem(web.Title, web.Url + "/AllWebNavigationCollection.cxml", web.Description, new ItemImage(new Uri(curWeb.Url + curTemplate.ImageUrl)), lstFacet.ToArray());
                web.Dispose();
               
            }

            return ColReturn;
        }
    }
}
